import { HW_AD_CONFIG, Ad_Type } from '../../frame/constants';
import util from '../tool/util';

cc.Class({
    extends: cc.Component,

    properties: {

        insert_Node: cc.Node,
        insert_img_sp: cc.Sprite,
        insert_icon: cc.Sprite,
        insert_title: cc.Label,
        insert_dec_lab: cc.Label,

        native_Node: cc.Node,
        native_icon: cc.Sprite,
        native_title: cc.Label,
        native_dec_lab: cc.Label,
        native_click_btn: cc.Label,

        bannerNode: cc.Node,
        cantClickMask_node: cc.Node,
    },

    initPage() {
        // he.ad.
    },

    updatePage(param) {
        // if (gg.data.inGuide) {
        //     this.uiBase.closePage()
        //     return
        // }
        this.insert_Node.active = false
        this.native_Node.active = false
        this.bannerNode.active = false

        this.adIndex = 0
        let isShowAd = param.isShowAd
        this.adType = param.adType
        let posion = param.position
        gg.data.reprotAd = false
        //是否开启了误点，华为 只展示插页的误点
        if (param.pit) {
            this.cantClickMask_node.active = true
        } else {
            this.cantClickMask_node.active = false
            this.adSwitch(isShowAd, this.adType, this.adIndex, posion)
        }
        //这个是华为专属的  后台切回时需要监听到的回调
        gg.eventManager.on('hwAd', () => {
            this.adSwitch(isShowAd, this.adType, this.adIndex, posion)
        }, this)
    },

    closeUI() {
        gg.eventManager.off('hwAd', () => {
            this.adSwitch(isShowAd, adType, this.adIndex, posion)
        }, this)
        this.adSwitch(false)
        this.insert_Node.active = false
        this.native_Node.active = false
        this.bannerNode.active = false
        console.log('广告销毁')
    },

    closeServer() {
        this.uiBase.closePage()
        console.log('guanbilea ')
    },

    cantClickMaskServer() {
        let time = util.randomNum(10, 15) / 10
        // this.cantClickMask_node.active = false
        this.scheduleOnce(() => {
            this.adSwitch(true, Ad_Type.INSERT)
            this.cantClickMask_node.active = false
        }, time)
    },

    adSwitch(status, adType, adIndex, posion) {
        if (status) {
            let ad = null
            switch (adType) {
                case Ad_Type.BANNER://Banner
                    //this.BannerInit(adIndex, posion)
                    this.showBanner = true
                    console.log('原bannerid', HW_AD_CONFIG.nativeAd[0])
                    ad = he.ad.getNativeJson(HW_AD_CONFIG.nativeAd[0])
                    if (ad)
                        this.NativeAdInit(ad, posion)
                    break;
                case Ad_Type.INSERT://插屏
                    this.showBanner = false
                    console.log('插页id', HW_AD_CONFIG.nativeAd[1])
                    ad = he.ad.getNativeJson(HW_AD_CONFIG.nativeAd[1])
                    if (ad)
                        this.InsertAdInit(ad, posion)
                    break;
                case Ad_Type.NATIVE://原生

                    break;
                default:
                    break;
            }
        }
        else {
            if (this.bannerNode.active) {
                he.ad.changeBanner(false, this.bannerNode)
                this.bannerNode.active = false
            }
        }
    },

    ///Banner设置
    BannerInit(adIndex, posion) {
        this.bannerNode.active = true
        this.bannerNode.setPosition(posion)
        if (HW_AD_CONFIG.bannerId[adIndex]) {
            he.log("Banner里面没有这个值，老哥快检查检查吧")
        }
        else {
            this.bannerNode.name = HW_AD_CONFIG.bannerId[adIndex]
            he.ad.changeBanner(true, this.bannerNode)
        }
    },

    ///原生设置
    NativeAdInit(info, posion) {
        let adInfo = info
        this.native_Node.active = true
        this.native_Node.getComponent(cc.Widget).bottom = posion
        this.native_Node.getComponent(cc.Widget).updateAlignment()
        if (adInfo) {
            this.insert_img_sp.active = false
            if (adInfo.imgUrlList[0]) {
                this.loadImg(this.native_icon, adInfo.imgUrlList[0], 1080, 607)
                this.native_icon.node.active = true
            } else {
                this.native_icon.node.active = false
            }
            console.log(adInfo)
            // this.native_click_btn.string = `${adInfo.clickBtnTxt}`
            this.native_title.string = `${adInfo.title}`
            this.native_dec_lab.string = `${adInfo.clickBtnTxt}`//`${adInfo.desc}`
        }
    },

    ///点击原生广告
    ClickServer() {
        gg.data.isClickAd = true
        if (this.showBanner) {
            he.ad.clickNative(HW_AD_CONFIG.nativeAd[0], this.closeServer.bind(this))
            this.closeServer()
        } else {
            he.ad.clickNative(HW_AD_CONFIG.nativeAd[1], this.closeServer.bind(this))
            this.closeServer()
        }
    },

    ///插页设置
    InsertAdInit(info, posion) {
        let adInfo = info
        this.insert_Node.active = true
        // this.insert_Node.setPosition(posion)
        console.log('插页信息', adInfo.imgUrlList[0])
        if (adInfo) {
            if (adInfo.imgUrlList[0]) {
                this.loadImg(this.insert_img_sp, adInfo.imgUrlList[0], 1080, 607)
                this.loadImg(this.insert_icon, adInfo.imgUrlList[0], 1080, 607)
                this.insert_icon.node.active = false
                this.insert_img_sp.node.active = true
            } else {
                this.insert_icon.node.active = false
                this.insert_img_sp.node.active = false
            }
            // this.insert_click_btn.string = `${adInfo.clickBtnTxt}`
            this.insert_title.string = `${adInfo.title}`
            this.insert_dec_lab.string = `${adInfo.clickBtnTxt}`//`${adInfo.desc}`
        }
    },

    ///加载图片
    loadImg(container, url, width, height) {
        if (url.length < 4) {
            return he.error(("图片长度不对错误"));
        }
        let ext = url.substring(url.length - 4, url.length)
        ext = ext.toLowerCase()
        if (ext == 'jpeg') ext = '.jpg'
        cc.assetManager.loadRemote(url, { ext: ext }, (err, texture) => {
            if (err) {
                he.error(err)
            }
            if (this.callFun) {
                this.callFun()
            }
            var sprite = new cc.SpriteFrame(texture);
            container.spriteFrame = sprite;
            container.node.setContentSize(width, height)
        });
    },
});
